package com.miguo.component;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.miguo.app.AppHelper;
import com.miguo.framework.view.ScrollingTextView;
import com.miguo.ui.R;
import com.mrper.util.ApkUtil;

/**
 * File Names: BaseHeadView
 * Describe:
 * User: LinXiaoZhi(87100854@qq.com)
 * Date: 2015-07-26
 * Time: 20:07
 */
public class BaseHeadView extends LinearLayout implements View.OnClickListener {

    private Paint mPaint;

    private Context mContext;

    private Activity mActivity;

    private RelativeLayout mBaseHeadRl;
    private ScrollingTextView mTitle;
    private ImageView mLeftBtn, mRightBtn;
    private TextView mRightTv;
    private View mDivider;

    private boolean isBackBtn = true;

    public BaseHeadView(Context context) {
        this(context, null);
    }

    public BaseHeadView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        mActivity = (Activity) context;
        inflate(mContext, R.layout.layer_base_head, this);
        findView();
        initView();
        //initPaint();
    }

    private void findView() {
        mBaseHeadRl = (RelativeLayout) findViewById(R.id.base_head_rl);
        mLeftBtn = (ImageView) findViewById(R.id.left_btn);
        mRightBtn = (ImageView) findViewById(R.id.right_btn);
        mRightTv = (TextView) findViewById(R.id.right_tv);
        mTitle = (ScrollingTextView) findViewById(R.id.title);
        mDivider = findViewById(R.id.divider);
    }

    private void initView() {
        mLeftBtn.setOnClickListener(this);
        mRightBtn.setOnClickListener(this);
        mTitle.setOnClickListener(this);
    }

    private void initPaint() {
        this.mPaint = new Paint();
        this.mPaint.setAntiAlias(true);
        this.mPaint.setStyle(Paint.Style.FILL);
        this.mPaint.setColor(ApkUtil.getColorById(mContext,R.color.bg_divider_dddddd));
        //this.mPaint.setColor(Color.BLACK);
        this.mPaint.setStrokeWidth(1.0F);
        setWillNotDraw(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_btn:
                if (!isBackBtn) {
                    AppHelper.StartMain(mActivity, "0");
                }
                if(!isInterceptImageBackEvent()) {
                    mActivity.overridePendingTransition(0, R.anim.push_right_out);
                    mActivity.finish();
                }
                break;
        }
    }

    //========================  2016-09-02 添加一下代码  ==========================================

    public interface OnInterceptImageBackEventListener {

        public boolean onInterceptImageBackEvent();

    }

    private OnInterceptImageBackEventListener onInterceptImageBackEventListener;

    /**  设置返回事件拦截回调  **/
    public void setOnInterceptImageBackEventListener(OnInterceptImageBackEventListener onInterceptImageBackEventListener){
        this.onInterceptImageBackEventListener = onInterceptImageBackEventListener;
    }

    /**  是否拦截返回按钮的事件 **/
    private boolean isInterceptImageBackEvent(){
        if(this.onInterceptImageBackEventListener != null){
            return this.onInterceptImageBackEventListener.onInterceptImageBackEvent();
        }
        return false;
    }

    //========================  2016-09-02 END  ==========================================

    public View getView() {
        return mBaseHeadRl;
    }

    public ImageView getLeftView() {
        return mLeftBtn;
    }

    public ScrollingTextView getTitleView() {
        return mTitle;
    }

    public ImageView getRightView() {
        return mRightBtn;
    }

    public TextView getRightTextView() {
        return mRightTv;
    }

    public View getDivider() {
        return mDivider;
    }

    public void setBackground(String color) {
        mBaseHeadRl.setBackgroundColor(Color.parseColor(color));
    }

    public void setTitle(String title) {
        mTitle.setText(title);
    }

    public void setTitle(int resid) {
        mTitle.setText(resid);
    }

    public void setActivity(Activity activity) {
        mActivity = activity;
    }

    public void setBackBtn(boolean BackBtn) {
        isBackBtn = BackBtn;
    }

//    public void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
//        canvas.drawLine(0.0F, getHeight() - 1, getWidth(), getHeight() - 1, this.mPaint);
//        setWillNotDraw(true);
//    }
}